package Diffenent;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class GetDiffenentFile {
	
	public static void main(String[] args) {
		//getDifferent("D:\\x","D:\\x2");
		renameDemo();
	}
	
	
	public static void renameDemo(){
		File oldFile= new File("D:\\x\\good.txt");
		File newFile= new File("D:\\x\\b.txt");
		String oldFileName=oldFile.getName();
		String newFileName=newFile.getName();
		if(!oldFileName.equals(newFileName)){//TODO
			//oldFile.renameTo(newFile);
			 if(oldFile.exists()&&!newFile.exists()){
				 System.out.println("oldFile.exists()&&!newFile.exists()");
				 oldFile.renameTo(newFile);
				}
				else if(oldFile.exists()&&newFile.exists()){
					oldFile.delete();
				}else{
					System.out.println("rename success");
				}	
		}
	}
	
	public static void getDifferent(String path1,String path2){
		
		File one = new File(path1);
		File two = new File(path2);
		
		String[] oneFileName = one.list();
		String[] twoFileName = two.list();
		List<String> list1= Arrays.asList(oneFileName);//转换成序列
		List<String> list2= Arrays.asList(twoFileName);//转换成序列
        		
		List diffList = diff(list1, list2);
		System.out.println("文件夹"+path1+"比文件"+path2+"多了：");
		for (int i = 0; i < diffList.size(); i++) {
			System.out.println(diffList.get(i) + " ");
		}
		System.out.println("文件夹"+path2+"比文件"+path1+"多了：");
		List diffList2 = diff(list2, list1);
		System.out.println("文件2比文件1多了：");
		for (int i = 0; i < diffList2.size(); i++) {
			System.out.println(diffList2.get(i) + " ");
		}
	}
	
	public static List diff(List ls, List ls2) {
		List list = new ArrayList(Arrays.asList(new Object[ls.size()]));
		Collections.copy(list, ls);
		list.removeAll(ls2);
		return list;
	}
}
